$(document).ready(function (){
    var product_url = '/index/web/product'
    var news_url = '/index/web/news'
    attrArray = []
    product_page_index = 1
    var cat_id = $(".product-category-id").val();
    var type_id = $(".product-type-id").val();
    var news_cat_id = '';
    var product_page = $("input[name='product-page']").val();
    $('input[type="checkbox"]').change(function (){
        if($(this).is(':checked')){
            var pid = $(this).data('id')
            attrArray.push(pid)
        }else{
            var pid = $(this).data('id')
            var i = attrArray.indexOf(pid)
            attrArray.splice(i,1)
        }

        page(attrArray,1,0,0)
    })

    $(".product-category").on("click",function (){
        console.log('click')
        type_id = $(this).data('type')
        cat_id = $(this).data('id')
        page(attrArray,1,type_id,cat_id)
        window.location.href = '/product/'+type_id+'-'+cat_id+'.html';
    })

    if(cat_id == 'all'){
        page(attrArray,0,4,0)
    }else{
        page(attrArray,0,type_id,cat_id)
    }

    function page(condition,state,type,cat_id){
        $.ajax({
            type: 'POST',
            url: product_url,
            data: {"page_index":product_page_index,"page_size":product_page,"attrIds":attrArray,"type":type,"cat_id":cat_id,"ajax":"1"},
            success: function(res){
                if(res.code == 0){
                    $(".product-total").html('共有产品：' + res.data.count)
                    if(state == 1){
                        $.ajax({
                            type: 'POST',
                            url: product_url,
                            data: {"page_index":1,"page_size":product_page,"attrIds":attrArray,"type":type,"cat_id":cat_id,ajax: "1"},
                            success: function(res){
                                $(".product-total").html('共有产品：' + res.data.count)
                                if(res.code == 0){
                                    if(res.data.list.length == 0){
                                        //alert('已经没有数据了')
                                        $(".product-total").html('共有产品：' + 0)
                                        $(".su-list2").html('');
                                    }else{
                                        $(".product-content").html('');
                                        $.each(res.data.list,function (k,v){
                                            var img_src = '';
                                            if(v.top_img !== 'undefined'){
                                                img_src = v.top_img
                                            }else{
                                                img_src = '';
                                            }
                                            var html = '<a href="/product-detail/'+v.id+'.html"  class="item wow fadeInUp animate">\n' +
                                                '        \n' +
                                                '                            <div class="pic-box">\n' +
                                                '                                <img src="' + img_src + '" alt="">\n' +
                                                '                            </div>\n' +
                                                '                            <div class="info-box">\n' +
                                                '                                <div class="tit">\n' +
                                                v.type_name +
                                                '                                </div>\n' +
                                                '                                <div class="txt">\n' + v.username +
                                                '                                </div>\n' +
                                                '                                <div class="btn_box">\n' +
                                                '                                    <div class="btn1 mar ">产品详情</div>\n' +
                                                '                                </div>\n' +
                                                '                            </div></a>';

                                            $(".product-content").append(html)
                                        })
                                        $(".su-list2").html($(".product-content").html());
                                    }
                                }else{
                                    alert(res.msg)
                                }
                            },
                            timeout: 2000,
                            error: function(jqXHR,textStatus){
                                console.log("请求失败，请稍后重试!");
                            }
                        })
                    }
                    layui.use('laypage', function(){
                        var laypage = layui.laypage;
                        //执行一个laypage实例
                        laypage.render({
                            elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                            ,limit: product_page
                            ,count: res.data.count //数据总数，从服务端得到
                            ,groups:5
                            ,jump: function(obj, first){
                                //首次不执行
                                if(!first){
                                    //ajax request begin
                                    $.ajax({
                                        type: 'POST',
                                        url: product_url,
                                        data: {"page_index":obj.curr,"page_size":obj.limit,"attrIds":attrArray,"type":type,"cat_id":cat_id,"ajax":"1"},
                                        success: function(res){
                                            $(".product-total").html('共有产品：' + res.data.count)
                                            if(res.code == 0){
                                                if(res.data.list.length == 0){
                                                    //alert('已经没有数据了')
                                                    $(".product-total").html('共有产品：' + 0)
                                                    $(".su-list2").html('');
                                                }else{
                                                    $(".product-content").html('');
                                                    $.each(res.data.list,function (k,v){
                                                        var img_src = '';
                                                        if(v.top_img !== 'undefined'){
                                                            img_src = v.top_img
                                                        }else{
                                                            img_src = '';
                                                        }
                                                        var html = '<a href="/product-detail/'+v.id+'.html"  class="item wow fadeInUp animate">\n' +
                                                            '        \n' +
                                                            '                            <div class="pic-box">\n' +
                                                            '                                <img src="' + img_src + '" alt="">\n' +
                                                            '                            </div>\n' +
                                                            '                            <div class="info-box">\n' +
                                                            '                                <div class="tit">\n' +
                                                            v.type_name +
                                                            '                                </div>\n' +
                                                            '                                <div class="txt">\n' + v.username +
                                                            '                                </div>\n' +
                                                            '                                <div class="btn_box">\n' +
                                                            '                                    <div class="btn1 mar ">产品详情</div>\n' +
                                                            '                                </div>\n' +
                                                            '                            </div></a>';

                                                    $(".product-content").append(html)
                                                    })
                                                    $(".su-list2").html($(".product-content").html());
                                                }
                                            }else{
                                                alert(res.msg)
                                            }
                                        },
                                        timeout: 2000,
                                        error: function(jqXHR,textStatus){
                                            console.log("请求失败，请稍后重试!");
                                        }
                                    })
                                }
                            }
                        });
                    })
                }else {
                    alert(res.msg)
                }
            },
            timeout: 2000,
            error: function(jqXHR,textStatus){
                console.log("请求失败，请稍后重试!");
            }
        })
    }

    $(".first-product-selected").on('click',function (){
        event.stopPropagation()
        var id = $(this).data('id')
        page(attrArray,1,1,id)
    })

    $(".second-product-selected").on('click',function (){
        event.stopPropagation()
        var id = $(this).data('id')
        page(attrArray,1,2,id)
    })

    $(".third-product-selected").on('click',function (){
        event.stopPropagation()
        var id = $(this).data('id')
        page(attrArray,1,3,id)
    })
})

